//
//  LDClassYuanQuan.m
//  Gift
//
//  Created by 李迪 on 15/11/13.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDClassYuanQuan.h"
#import "LDYuanQuanCell.h"
#import "BaseHeader.h"
#import "LDClassYuanQuanDetails.h"

@interface LDClassYuanQuan ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, assign)NSInteger page;
@end

@implementation LDClassYuanQuan

-(void)dealloc{
    self.tab.delegate = nil;
    self.tab.dataSource = nil;
    [self.tab release];
    [self.array release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}


- (void)carateTab{

    self.tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.tab registerClass:[LDYuanQuanCell class] forCellReuseIdentifier:@"LDClassYuan"];
    self.tab.delegate = self;
    self.tab.dataSource = self;
    self.tab.rowHeight = SCREEN_HEIGHT / 4;
    [self.view addSubview:self.tab];
    [self.tab release];
}
#pragma mark - tab协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   LDYuanQuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDClassYuan"];
    ClassificationModel *model = [_array objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassificationModel *model = [_array objectAtIndex:indexPath.row];
    LDClassYuanQuanDetails *detaisl = [[LDClassYuanQuanDetails alloc]init];
    detaisl.str = model.content_url;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detaisl animated:YES];
    
    [detaisl release];
}


#pragma mark - 数据解析
- (void)handleData:(NSInteger)number{
    
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?ad=1&gender=1&generation=0&limit=20&offset=%ld",self.webInt,number];
    [PFAFNetworking getUrl:str body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        if (number == 0) {
            
            self.array = [[NSMutableArray array]init];
        }
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"items"]) {
            ClassificationModel *model = [ClassificationModel modelSetDic:dic];
            [_array addObject: model];
        }
        
        [self.tab reloadData];
        [self.tab.header endRefreshing];
        [self.tab.footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}







#pragma mark - 上拉刷新下拉加载
- (void)addHarder{
    
    self.tab.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self handleData:_page];
    }];
    
    [self.tab.header beginRefreshing];
}

-(void)addFoot{
    self.tab.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page = self.page + 20;
        [self handleData:_page];
    }];
    [self.tab.footer beginRefreshing];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self carateTab];
    [self handleData:0];
    [self addHarder];
    [self addFoot];
    self.title = @"精选";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
